const { default: api } = require("../../utils/api");
const message = require("../../utils/message");

// pages/category/category.js
Page({

    /**
     * 页面的初始数据
     */
    data: {

        category_id:null,
        select_index: 0,
        no_data:false,
        has_load:false,
        page_index:1,
        scroll_left:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        if(options.id){

            this.setData({
                category_id: options.id
            })
            
            this.init();
        }

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    init:function () {
        var params = {};
        params.category_id = this.data.category_id;


        var page_index = this.data.page_index;
        if(this.has_load){
            page_index++;
        }

        params.page_index = page_index;

        var me  = this;

        message.show_loading();
        api.product.getCategoryIndexData( params , function (response) {
            message.hide_loading();
            if( response.status==1 ){

                if( response.data.category ){

                    var title = response.data.category.category_name
                    wx.setNavigationBarTitle({
                      title: title,
                    })
                }
                me.setData({
                    category: response.data.category,
                    category_list: response.data.category_list,
                    product_list:  response.data.product_list,
                    has_load:true
                });
            }else{
                message.show_error( response.message );
            }
        })
        
    },
    selectNavItem:function (e) {
        var index = e.currentTarget.dataset.index;
        var left = index*80;

        if(left<360){
            left=0;
        }
        this.setData({
            select_index: index,
            scroll_left:left
        })
    },
    getCategoryProductList:function (params) {
        var me = this;

        var params = {}
        params.category_id = this.data.category_id;

        var page_index = this.data.page_index;

        page_index++;

        params.page_index = page_index;


        message.show_loading();
        api.product.getProductListByCategory( params , function (response) {
            message.hide_loading();

            if( response.status==1 ){
                var product_list = me.data.product_list;

                product_list = product_list.concat( response.data )

                var no_data = false;
                if(response.data.length<10){
                    no_data = true
                }

                me.setData({
                    product_list: product_list,
                    no_data: no_data,
                    page_index:page_index
                })
            }else{

                message.show_error( response.message )
            }
            
        })

        
        
    },
    currentChange:function (e) {
        var current = e.detail.current;

        var left = current*80;

        if(left<360){
            left=0;
        }

        this.setData({
            select_index:current,
            scroll_left:left
        })
    }
})